/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

   StdDataTypesCoderTest.cpp. Created on: 10.06.2009 13:52:07
*/

#include "smartobjects/network/tcp/TCPFactory.hpp"
#include "cpputils/TestUtils.hpp"
#include "AcceptorListenerTester.hpp"

using namespace smartobjects;

BOOST_AUTO_TEST_CASE ( TestCreateAcceptor ) {
	boost::intrusive_ptr<INetworkFactory> poFactory ( new TCPFactory(), false );
	boost::intrusive_ptr<URI> poServerURI ( new URI("tcp://localhost:3333"), false );
	boost::intrusive_ptr<IAcceptor> poAcceptor ( poFactory->createAcceptor( poServerURI.get() ), false);
	BOOST_REQUIRE ( poAcceptor.get()!=NULL  );

	poAcceptor->start();
	poAcceptor->stop();
}

BOOST_AUTO_TEST_CASE ( TestCreateConnector ) {
	boost::intrusive_ptr<INetworkFactory> poFactory ( new TCPFactory(), false );
	boost::intrusive_ptr<URI> poServerURI ( new URI("tcp://localhost:3333"), false );
	boost::intrusive_ptr<IConnector> poConnector ( poFactory->createConnector(poServerURI.get()), false );
	BOOST_REQUIRE ( poConnector.get()!=NULL  );
	BOOST_REQUIRE_THROW ( poConnector->connect(), IOException );
}


BOOST_AUTO_TEST_CASE ( TestAcceptorListener ) {
	boost::intrusive_ptr<INetworkFactory> poFactory ( new TCPFactory(), false );
	boost::intrusive_ptr<URI> poServerURI ( new URI("tcp://localhost:3333"), false );
	boost::intrusive_ptr<IAcceptor> poAcceptor ( poFactory->createAcceptor( poServerURI.get() ), false);
	BOOST_REQUIRE ( poAcceptor.get()!=NULL  );
	AcceptorListenerTester::SharedPtr poListener ( new AcceptorListenerTester() );
	poAcceptor->addListener(poListener.get());

	poAcceptor->start();

	boost::intrusive_ptr<IConnector> poConnector1 ( poFactory->createConnector(poServerURI.get()), false );
	BOOST_REQUIRE ( poConnector1.get()!=NULL  );

	boost::intrusive_ptr<IConnector> poConnector2 ( poFactory->createConnector(poServerURI.get()), false );
	BOOST_REQUIRE ( poConnector2.get()!=NULL  );

	boost::mutex::scoped_lock oLock(poListener->getMonitor());
	poConnector1->connect();
	poListener->waitForAnyEvent(oLock, "Waiting for accept connection #1");
	oLock.unlock();
	BOOST_REQUIRE ( poListener->getConnections().size() == 1 );

	oLock.lock();
	poConnector2->connect();
	poListener->waitForAnyEvent(oLock, "Waiting for accept connection #2");
	oLock.unlock();
	BOOST_REQUIRE ( poListener->getConnections().size() == 2 );

	poAcceptor->stop();
	poAcceptor->delListener(poListener.get());
}

BOOST_AUTO_TEST_CASE ( TestConnectorListener ) {
	boost::intrusive_ptr<INetworkFactory> poFactory ( new TCPFactory(), false );
	boost::intrusive_ptr<URI> poServerURI ( new URI("tcp://localhost:3333"), false );
	boost::intrusive_ptr<IAcceptor> poAcceptor ( poFactory->createAcceptor( poServerURI.get() ), false);

	poAcceptor->start();

	ConnectionListenerTester::SharedPtr poListener ( new ConnectionListenerTester() );

	boost::intrusive_ptr<IConnector> poConnector ( poFactory->createConnector(poServerURI.get()), false );

	poConnector->addConnectionListener(poListener.get());

	boost::mutex::scoped_lock oLock(poListener->getMonitor());
	poConnector->connectAsync();
	poListener->waitForAnyEvent(oLock, "Waiting to connect");
	oLock.unlock();
	BOOST_REQUIRE ( poListener->isConnected() );

	poConnector->delConnectionListener(poListener.get());

	poAcceptor->stop();
}

BOOST_AUTO_TEST_CASE ( TestExchangeData ) {
	boost::intrusive_ptr<INetworkFactory> poFactory ( new TCPFactory(), false );
	boost::intrusive_ptr<URI> poServerURI ( new URI("tcp://localhost:3333"), false );

	AcceptorListenerTester::SharedPtr poAcceptorListener ( new AcceptorListenerTester() );

	boost::intrusive_ptr<IAcceptor> poAcceptor ( poFactory->createAcceptor( poServerURI.get() ), false);

	poAcceptor->addListener(poAcceptorListener.get());


	poAcceptor->start();

	ConnectionListenerTester::SharedPtr poConnectorListener ( new ConnectionListenerTester() );

	boost::intrusive_ptr<IConnector> poConnector ( poFactory->createConnector(poServerURI.get()), false );

	poConnector->addConnectionListener(poConnectorListener.get());

	boost::mutex::scoped_lock oConnectorLock(poConnectorListener->getMonitor());
	boost::mutex::scoped_lock oAcceptorLock(poAcceptorListener->getMonitor());

	poConnector->connectAsync();

	poConnectorListener->waitForAnyEvent(oConnectorLock, "Waiting to connect");
	poAcceptorListener->waitForAnyEvent(oAcceptorLock, "Waiting to accept");

	oAcceptorLock.unlock();
	oConnectorLock.unlock();

	BOOST_REQUIRE ( poConnectorListener->isConnected() );
	BOOST_REQUIRE ( poAcceptorListener->getConnections().size() == 1 );

	const unsigned char aBuffer[] =  { 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
	ByteBuffer oBuffer( sizeof(aBuffer) );
	oBuffer.append(aBuffer, sizeof(aBuffer));

	boost::mutex::scoped_lock oAcceptorConnectionLock( poAcceptorListener->getConnectionListener().getMonitor() );
	poConnector->send(&oBuffer);
	poAcceptorListener->getConnectionListener().waitForAnyEvent(oAcceptorConnectionLock, "Waiting to receive on acceptor connection");
	oAcceptorConnectionLock.unlock();
	BOOST_REQUIRE ( poAcceptorListener->getConnectionListener().getLastReceivedMessage()!=NULL );
	checkBuffers( &oBuffer, poAcceptorListener->getConnectionListener().getLastReceivedMessage() );

	poConnector->delConnectionListener(poConnectorListener.get());

	poAcceptor->stop();
}
